package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.Com.AddressBook;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.common.vo.AddressBookDto;
import com.itheima.common.vo.AddressBookVo;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.service.AddressBookService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public List<AddressBook> list() {
        List<AddressBook> list = addressBookMapper.list();
        return list;
    }

    @Override
    public AddressBookVo
    add(AddressBookVo addressBookVo) {

        addressBookVo.setId(IdUtil.getSnowflakeNextIdStr());
        String id = ThreadLocalUtil.getId();
        System.out.println(id);
        addressBookVo.setUserId(ThreadLocalUtil.getId());
        int add = addressBookMapper.add(addressBookVo);
        AddressBookVo all = addressBookMapper.findAll(id);
        return all;
    }

    @Override
    public AddressBook getByid(String id) {
        AddressBook byId = addressBookMapper.getById(id);
        return byId;
    }

    @Override
    public AddressBook getmoren() {

        AddressBook getmoren = addressBookMapper.getmoren();
        if (getmoren.getIsDefault() == 1) {
            return getmoren;
        }
        return null;

    }

    @Override
    public boolean updateMoren(AddressBookDto addressBookDto) {
        //通过查询得到默认地址值为一的数据
        AddressBook addressBookMapper1 = addressBookMapper.find1();
        //判断不为空
        if (addressBookMapper1!=null){
            //修改默认地址值为0
            addressBookMapper.set();
        }
//通过查询得到id相同地址值等于一的数据
        AddressBook addressBook = addressBookMapper.find(addressBookDto.getId());
        //判断
        if (addressBook==null){
            //修改数据地址值为一
           return addressBookMapper.updateMoren(addressBookDto);
        }
        //修改失败
        return false;


    }

    @Override
    public boolean updateById(AddressBook addressBook) {
        boolean b = addressBookMapper.updateById(addressBook);
        return b;
    }

    @Override
    public boolean delete(String ids) {
        boolean delete = addressBookMapper.delete(ids);
        return delete;
    }
}